﻿//SmartComHelper.cs
//Copyright (c) 2013 StockSharp LLC, all rights reserved.
//This code module is part of StockSharp library.
//This code is licensed under the GNU GENERAL PUBLIC LICENSE Version 3.
//See the file License.txt for the license details.
//More info on: http://stocksharp.com

namespace StockSharp.Smart
{
	using System;

	using StockSharp.BusinessEntities;

	using StClientLib;

	static class SmartComHelper
	{
		public static bool IsReject(this StOrder_State state)
		{
			switch (state)
			{
				case StOrder_State.StOrder_State_ContragentReject:
				case StOrder_State.StOrder_State_ContragentCancel:
				case StOrder_State.StOrder_State_SystemReject:
				case StOrder_State.StOrder_State_SystemCancel:
					return true;
				default:
					return false;
			}
		}

		public static OrderDirections? ToOrderDirection(this StOrder_Action action)
		{
			switch (action)
			{
				case StOrder_Action.StOrder_Action_Buy:
					return OrderDirections.Buy;
				case StOrder_Action.StOrder_Action_Sell:
					return OrderDirections.Sell;
				case StOrder_Action.StOrder_Action_Short:
					return OrderDirections.Sell;
				case StOrder_Action.StOrder_Action_Cover:
					return OrderDirections.Buy;
				case 0:
					return null;
				default:
					throw new ArgumentOutOfRangeException("action", action, "Неизвестное значение для направления заявки.");
			}
		}

		public static StOrder_Type GetSmartOrderType(this Order order)
		{
			if (order == null)
				throw new ArgumentNullException("order");

			switch (order.Type)
			{
				case OrderTypes.Limit:
					return StOrder_Type.StOrder_Type_Limit;
				case OrderTypes.Market:
					return StOrder_Type.StOrder_Type_Market;
				case OrderTypes.Conditional:
					return order.Price != 0 ? StOrder_Type.StOrder_Type_StopLimit : StOrder_Type.StOrder_Type_Stop;
				default:
					throw new ArgumentOutOfRangeException("order");
			}
		}

		public static OrderTypes ToOrderType(this StOrder_Type smartType)
		{
			switch (smartType)
			{
				case StOrder_Type.StOrder_Type_Market:
					return OrderTypes.Market;
				case StOrder_Type.StOrder_Type_Limit:
					return OrderTypes.Limit;
				case StOrder_Type.StOrder_Type_Stop:
				case StOrder_Type.StOrder_Type_StopLimit:
					return OrderTypes.Conditional;
				default:
					throw new ArgumentOutOfRangeException("smartType", smartType, "Неизвестное значение для типа заявки.");
			}
		}

		public static StOrder_Action GetSmartAction(this SmartTrader trader, Order order)
		{
			if (trader == null)
				throw new ArgumentNullException("trader");

			if (order == null)
				throw new ArgumentNullException("order");

			if (order.Security.ExchangeBoard == ExchangeBoard.Micex)
			{
				var position = trader.GetPosition(order.Portfolio, order.Security);

				if (position == null)
					return order.Direction == OrderDirections.Buy ? StOrder_Action.StOrder_Action_Buy : StOrder_Action.StOrder_Action_Short;

				if (order.Direction == OrderDirections.Buy)
				{
					return position.CurrentValue >= 0 ? StOrder_Action.StOrder_Action_Buy : StOrder_Action.StOrder_Action_Cover;
				}
				else
				{
					return (position.CurrentValue - order.Volume) >= 0 ? StOrder_Action.StOrder_Action_Sell : StOrder_Action.StOrder_Action_Short;
				}
			}
			else
				return order.Direction == OrderDirections.Buy ? StOrder_Action.StOrder_Action_Buy : StOrder_Action.StOrder_Action_Sell;
		}
	}
}